package lhdx4;

public class Xueshechaz {
    String name;
    String dirTeacher;
    String startDate;

    String[] stus = new String[30];

    public void add(String stuname) {
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] == null) {
                stus[i] = stuname;
                return;
            }
        }
        System.out.println("人已满");
    }

    public void showInformation() {
        System.out.println("班级信息如下：");
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] == null)
                break;
            System.out.print(stus[i] + "   ");
        }
        System.out.println("");
    }

    public boolean searchName(String name) {
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] == null) {
                return false;
            }
            if (stus[i].equals(name))
                return true;
        }
        return false;
    }

    //根据位置查找学生姓名；
    public String find(int index) {
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] == null) {
                return "未找到";
            }
            if (i == index - 1)
                return "已找到：" + stus[i];
        }
        return "未找到";
    }

//实现在修改学生姓名功能，方法声明如下：public boolean update(String oldName,String newName)
//提示：要考虑到oldName(老名字）不存在的情况。
    public boolean update(String oldName, String newName) {
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] == null) {
                return false;
            }
            if (stus[i].equals(oldName)){
                stus[i]=newName;
                return true;
            }
        }
        return false;
    }



    //添加获得班级人数的方法,方法声明如下：public int getNum()
    public int getNum(){
        int a=0;
        for (int i=0;i<stus.length;i++){
            if (stus[i]==null){
                break;
            }
            a++;
        }
        return a;
    }
}

